package com.etown.util;

public class StringUtil {
	private StringUtil(){
		
	}
	
	/**
	 * 判断字符串是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str){
		if(str==null||str.length()==0||"".equals(str)){
			return true;
		}
		return false;
	}
	
	/**
	 * 对字符串模板进行替换
	 * 
	 * @param tplStr
	 * @param content
	 */
	public static String format(String tplStr,String[] content){
		if(content==null||content.length==0){
			return "";
		}
		for(int i=0;i<content.length;i++){
			tplStr=tplStr.replace("{"+i+"}", content[i]);
		}
		return tplStr;
	}
	
	/**
	 * 检测字符串编码
	 * 只能检测这些类型:"utf-8","gbk","gb2312","ISO-8859-1"
	 * @param str
	 * @return
	 */
	public static String getEncoding(String str){
		String[] encodingStr=new String[]{"utf-8","gbk","gb2312","ISO-8859-1"};
		for(int i=0;i<encodingStr.length;i++){
			String ec=encodingStr[i];
			try{
				if(str.equals(new String(str.getBytes(ec),ec))){
					return ec;
				}
			}catch (Exception e) {
			}
		}
		return "";
	}
}
